Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Web.UI
Imports System.Web.UI.WebControls






Public Class AccessibilityController


    ''' <summary>
    ''' When a gridview is being edited, set a tooltip property to any editable control that
    ''' has a tooltip property.  Use the header text of the gridview to set the tooltip text.
    ''' </summary>
    ''' <param name="row"></param>
    ''' <param name="grd"></param>
    ''' <remarks></remarks>
    Public Shared Sub SetGridViewEditToolTips(ByVal row As GridViewRow, ByVal grd As GridView)
        For i As Integer = 0 To grd.Columns.Count - 1
            If row.Cells(i).Controls.Count = 0 Then Exit Sub
            For j As Integer = 0 To row.Cells(i).Controls.Count - 1
                Try
                    Dim ctl As Control = row.Cells(i).Controls(j)
                    Dim tooltipText As String = "edit " & grd.Columns(i).HeaderText.ToLower()
                    'set the tooltip property
                    If TypeOf (ctl) Is TextBox Then
                        Dim txt As TextBox = CType(ctl, TextBox)
                        txt.ToolTip = tooltipText
                    ElseIf TypeOf (ctl) Is CheckBox Then
                        Dim chk As CheckBox = CType(ctl, CheckBox)
                        chk.ToolTip = tooltipText
                    ElseIf TypeOf (ctl) Is DropDownList Then
                        Dim ddl As DropDownList = CType(ctl, DropDownList)
                        ddl.ToolTip = tooltipText
                    End If

                Catch ex As Exception
                    'do nothing
                End Try
            Next
        Next
    End Sub
    ''' <summary>
    ''' For Accessibility: all sortable gridview column headers are rendered as a link that executes javascript.
    ''' This method, for all sortable headers in a grid, adds a blank image with an alt tag advising user that the link sorts the column.
    ''' </summary>
    ''' <param name="grd">The gridview to insert the blank image with alt tag to</param>
    ''' <remarks>This method is called inside the master page</remarks>
    Public Shared Sub AddImageToSortableGridHeaders(ByVal grd As GridView)
        Dim appPath As String = AppPathName()

        If grd.HeaderRow Is Nothing Then Exit Sub
        If grd.ShowHeader = False Then Exit Sub
        For i As Integer = 0 To grd.Columns.Count - 1
            If Not String.IsNullOrEmpty(grd.Columns(i).SortExpression) Then
                Dim headerCell As System.Web.UI.WebControls.TableCell = grd.HeaderRow.Cells(i)
                If headerCell.Controls.Count > 0 Then
                    Dim headerLinkButton As LinkButton = headerCell.Controls(0)
                    If Not headerLinkButton.Text.Contains("1x1.gif") Then
                        headerLinkButton.Text = String.Concat(headerLinkButton.Text, " <img src=""", appPath, "/images/1x1.gif"" ALT=""sort column"" border=""0"">")
                    End If
                End If
            End If
        Next
    End Sub

    Private Shared Function AppPathName() As String
        Dim prefixString As String = ""
        Dim isSecure As Boolean
        If System.Web.HttpContext.Current.Request.ServerVariables("HTTPS") = "on" Then
            prefixString = "https://"
            isSecure = True
        Else
            prefixString = "http://"
            isSecure = False
        End If

        Return prefixString & GetHostName(isSecure) & _
               System.Web.HttpContext.Current.Request.ApplicationPath

    End Function

    Private Shared Function GetHostName(ByVal isSecure As Boolean) As String
        If isSecure Or System.Web.HttpContext.Current.Request.Url.Port = 80 Then
            Return System.Web.HttpContext.Current.Request.Url.Host
        Else
            Return System.Web.HttpContext.Current.Request.Url.Host() & ":" & System.Web.HttpContext.Current.Request.Url.Port.ToString()
        End If
    End Function


End Class
